package gui;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

import java.awt.BorderLayout;
import javax.swing.JComboBox;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import observer.Broker;
import observer.Observer;
import utility.Auto;
import javax.swing.JTable;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;

public class StatisticheAutoPanel extends JPanel implements ActionListener, Observer {

	private Broker broker;
	private JTable carsTable,carTableDetails;
	private TableModelCar carsTableModel;
	private JScrollPane scrollPaneTable;
	private JButton btnUpdateTable;
	private Icon refreshTableIcon;
	private JLabel lblDettagliAuto;
	private TableModelCardetails modelCarDetails;


	/**
	 * Create the panel.
	 */
	public StatisticheAutoPanel(final Broker broker) {

		refreshTableIcon = new ImageIcon("img/refreshData.jpg");

		this.broker = broker;

		broker.addObserver(this);

		this.setPreferredSize(new Dimension(250, 600));

		GridBagLayout gridBagLayout = new GridBagLayout();

		gridBagLayout.columnWidths = new int[]{158, 0};
		gridBagLayout.rowHeights = new int[]{0, 209, 32, 0, 184, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		carsTable = new JTable();
		carsTableModel = new TableModelCar();
		carsTable.setModel(carsTableModel);
		scrollPaneTable = new JScrollPane(carsTable);

		GridBagConstraints gbc_carsTable = new GridBagConstraints();
		gbc_carsTable.insets = new Insets(0, 0, 5, 0);
		gbc_carsTable.fill = GridBagConstraints.BOTH;
		gbc_carsTable.gridx = 0;
		gbc_carsTable.gridy = 1;
		add(scrollPaneTable, gbc_carsTable);


		JLabel lblStatiticheMacchine = new JLabel("TABELLA MACCHINE");
		lblStatiticheMacchine.setFont(new Font("Tahoma", Font.BOLD, 13));
		GridBagConstraints gbc_lblStatiticheMacchine = new GridBagConstraints();
		gbc_lblStatiticheMacchine.insets = new Insets(0, 0, 5, 0);
		gbc_lblStatiticheMacchine.gridx = 0;
		gbc_lblStatiticheMacchine.gridy = 0;
		add(lblStatiticheMacchine, gbc_lblStatiticheMacchine);

		lblDettagliAuto = new JLabel("DETTAGLIO AUTO");
		lblDettagliAuto.setFont(new Font("Tahoma", Font.BOLD, 13));
		GridBagConstraints gbc_lblDettagliAuto = new GridBagConstraints();
		gbc_lblDettagliAuto.insets = new Insets(0, 0, 5, 0);
		gbc_lblDettagliAuto.gridx = 0;
		gbc_lblDettagliAuto.gridy = 2;
		add(lblDettagliAuto, gbc_lblDettagliAuto);
		modelCarDetails = new TableModelCardetails();


		carTableDetails = new JTable();
		carTableDetails.setModel(modelCarDetails);


		GridBagConstraints gbc_statisticList = new GridBagConstraints();
		gbc_statisticList.insets = new Insets(0, 0, 5, 0);
		gbc_statisticList.fill = GridBagConstraints.BOTH;
		gbc_statisticList.gridx = 0;
		gbc_statisticList.gridy = 3;
		add(carTableDetails, gbc_statisticList);



		btnUpdateTable = new JButton(refreshTableIcon);
		GridBagConstraints gbc_btnUpdateTable = new GridBagConstraints();
		gbc_btnUpdateTable.gridx = 0;
		gbc_btnUpdateTable.gridy = 4;

		btnUpdateTable.addActionListener(this);
		add(btnUpdateTable, gbc_btnUpdateTable);


		GridBagConstraints gbc_comboBoxAutos = new GridBagConstraints();
		gbc_comboBoxAutos.gridwidth = 2;
		gbc_comboBoxAutos.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBoxAutos.insets = new Insets(0, 0, 5, 5);
		gbc_comboBoxAutos.gridx = 3;
		gbc_comboBoxAutos.gridy = 1;

		GridBagConstraints gbc_listAutosProprieties = new GridBagConstraints();
		gbc_listAutosProprieties.gridwidth = 2;
		gbc_listAutosProprieties.insets = new Insets(0, 0, 0, 5);
		gbc_listAutosProprieties.fill = GridBagConstraints.BOTH;
		gbc_listAutosProprieties.gridx = 3;
		gbc_listAutosProprieties.gridy = 3;



		carsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// TODO Auto-generated method stub

				//Auto car = broker.getAutoBroker().get(carsTable.getSelectedRow());
				
				Auto car = broker.getAutoBroker().get((int)carsTable.getValueAt(carsTable.getSelectedRow(), 0));

				if(car.getKmTot() > 0)
				{
					
					modelCarDetails.setValueAt(car.getId(), 0, 1);
					modelCarDetails.setValueAt(car.getStreetStart(), 1, 1);
					modelCarDetails.setValueAt(car.getStreetEnd(), 2, 1);
					modelCarDetails.setValueAt(car.isAlive(), 3, 1);
					modelCarDetails.setValueAt(car.getWay().get(0).getStreetName(), 4, 1);
					modelCarDetails.setValueAt(car.getKm(), 5, 1);
					modelCarDetails.setValueAt(car.getSpeed(), 6, 1);
					modelCarDetails.setValueAt(car.getMaxSpeed(), 7, 1);
					modelCarDetails.setValueAt(car.getKmTot(), 8, 1);
					modelCarDetails.setValueAt(car.getType(), 9, 1);
					modelCarDetails.setValueAt(car.getVelocitaMedia()*10, 10, 1);
				
				}

				carTableDetails.updateUI();

			}
		});

	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == btnUpdateTable){
			carsTable.updateUI();
			carTableDetails.updateUI();
		}

	}


	@Override
	public void update(Object object) {
		if(object.equals("refreshAutoBroker"))
			carsTable.updateUI();
	}



	private class TableModelCardetails extends DefaultTableModel{

		private static final long serialVersionUID = 1L;


		Object [][] data = {{"ID",""},
				{"STRADA PARTENZA",""},
				{"STRADA ARRIVO",""},
				{"ALIVE",""},
				{"STRADE CORRENTE",""},
				{"KM CORRENTE",""},
				{"VELOCITA",""},
				{"VELOCITA MASSIMA",""},
				{"KM PERCORSI",""},
				{"TIPO VETTURA",""},
				{"VELOCITA MEDIA",""}
		};



		@Override
		public boolean isCellEditable(int row, int column) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return 2;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return 11;
		}

		@Override
		public Object getValueAt(int row, int column) {
			// TODO Auto-generated method stub
			return data[row][column];
		}

		@Override
		public void setValueAt(Object aValue, int row, int column) {
			// TODO Auto-generated method stub
			data[row][column] = aValue;
		}

	}

	private class TableModelCar extends  DefaultTableModel {

		private static final long serialVersionUID = 1L;


		private String[] columnNames= {"ID", "ALIVE"};

		@Override
		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public int getRowCount() {
			return broker.getAutoBroker().size();
		}

		@Override
		public boolean isCellEditable(int row, int column) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public Object getValueAt(int row, int col) {

			switch (col) {
			case 0:
				return broker.getAutoBroker().get(row).getId();
			case 1:
				return broker.getAutoBroker().get(row).isAlive();
			default:
				return null;
			}

		}

		@Override
		public String getColumnName(int column) {
			return columnNames[column];
		}
	}

}


